/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import javax.swing.JOptionPane;
import model.dao.DaoAdm;
import model.dao.DaoAluno;
import model.dao.DaoProfessor;
import model.dominio.Adm;
import model.dominio.Aluno;
import model.dominio.Professor;

/**
 *
 * @author Ronan
 */
public class TelaLogin extends javax.swing.JFrame {

    /**
     * Creates new form TelaLogin
     */
    public TelaLogin() {
        initComponents();
        DaoAdm admin = new DaoAdm();
        System.out.println(admin.buscarAdms(new Adm()).size());
        try {
            if (admin.buscarAdms(new Adm()).isEmpty()) {
                JOptionPane.showMessageDialog(null, "Não há administrador cadastrado.\n Cadastre agora.", "", JOptionPane.WARNING_MESSAGE);
                GuiAdmCadastrar painel = new GuiAdmCadastrar();
                boxCadastroAdm.add(painel);
                painel.setVisible(true);
                boxCadastroAdm.setVisible(true);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        boxLogin = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        BEntrar = new javax.swing.JButton();
        cpf = new javax.swing.JFormattedTextField();
        senha = new javax.swing.JPasswordField();
        tipoUsuario = new javax.swing.JComboBox();
        boxCadastroAdm = new javax.swing.JInternalFrame();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        boxLogin.setTitle("Login");
        boxLogin.setVisible(true);
        boxLogin.getContentPane().setLayout(null);

        jLabel1.setText("Senha");
        boxLogin.getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 100, 50, 17);

        jLabel2.setText("CPF");
        boxLogin.getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 60, 60, 17);

        BEntrar.setText("Entrar");
        BEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEntrarActionPerformed(evt);
            }
        });
        boxLogin.getContentPane().add(BEntrar);
        BEntrar.setBounds(90, 140, 90, 30);

        try {
            cpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxLogin.getContentPane().add(cpf);
        cpf.setBounds(60, 50, 160, 30);
        boxLogin.getContentPane().add(senha);
        senha.setBounds(60, 90, 160, 30);

        tipoUsuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Professor", "Aluno" }));
        tipoUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoUsuarioActionPerformed(evt);
            }
        });
        boxLogin.getContentPane().add(tipoUsuario);
        tipoUsuario.setBounds(60, 10, 160, 27);

        boxLogin.setBounds(610, 210, 280, 220);
        jDesktopPane1.add(boxLogin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        boxCadastroAdm.setClosable(true);
        boxCadastroAdm.setVisible(false);
        boxCadastroAdm.getContentPane().setLayout(new java.awt.GridLayout(1, 1));
        boxCadastroAdm.setBounds(0, 0, 471, 300);
        jDesktopPane1.add(boxCadastroAdm, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jDesktopPane1);
        jDesktopPane1.setBounds(0, 0, 1350, 760);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1356)/2, (screenSize.height-788)/2, 1356, 788);
    }// </editor-fold>//GEN-END:initComponents

    private void BEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEntrarActionPerformed
        switch (tipoUsuario.getSelectedIndex()) {
            case 0:
                Adm adm;
                DaoAdm a = new DaoAdm();
                adm = a.buscarLogin(cpf.getText(), new String(senha.getPassword()));
                if (adm!=null){
                    this.dispose();
                    AdmPrincipal tela=new AdmPrincipal();
                    tela.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null, "Usuário não encontrado");
                }
                break;
            case 1:
                Professor prof1 ;
                DaoProfessor prof2 = new DaoProfessor();
                prof1 = prof2.buscarLogin(cpf.getText(), new String(senha.getPassword()));
                if (prof1!=null){
                    this.dispose();
                    ProfPrincipal tela=new ProfPrincipal();
                    tela.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null, "Usuário não encontrado");
                }
                break;
            case 2:
                Aluno aluno;
                DaoAluno aluno2 = new DaoAluno();
                aluno = aluno2.buscarLogin(cpf.getText(), new String(senha.getPassword()));
                if (aluno!=null){
                    this.dispose();
                    AlunoPrincipal tela=new AlunoPrincipal();
                    tela.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null, "Usuário não encontrado");
                }
                break;
        }
    }//GEN-LAST:event_BEntrarActionPerformed

    private void tipoUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tipoUsuarioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new TelaLogin().setVisible(true);





            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BEntrar;
    private javax.swing.JInternalFrame boxCadastroAdm;
    private javax.swing.JInternalFrame boxLogin;
    private javax.swing.JFormattedTextField cpf;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField senha;
    private javax.swing.JComboBox tipoUsuario;
    // End of variables declaration//GEN-END:variables
}
